const getActionByNodeType = (nodeType, node) => {
  if (nodeType === 'start') {
    return 'start using="" intergration=false'
  } else if (nodeType === 'end') {
    return 'end using="" intergration=false'
  } else if (nodeType === 'switch') {
    console.log(Object.values(node.data.input.value));
    
    return `branch cnd=<% $.${
      Object.values(node.data.input.value)[0][1]
    } %> app=\"\" script=\"\" isSwitch=true using=\"\" integration=false`
  } else {
    let inputArr = []
    for (const key in node.data.input.value) {
      const value = node.data.input.value[key]
      if (node.data.globalVariables.includes(key)) {
        inputArr.push(`${key}=<% $.${value[value.length - 1] || ''} %>`)
      } else {
        inputArr.push(`${key}=<% $.${value || ''} %>`)
      }
    }
    return `${node.data.name} ${inputArr.join(' ')} app="${
      node.data.integration_id
    }" script="" isSwitch=false using="${
      node.data.instance_id
    }" integration=true`
  }
}

const getPublishByNodeType = (nodeType, node) => {
  if (nodeType === 'start') {
    return {
      data: '<% task(start).result.content.data %>'
    }
  } else if (nodeType === 'end') {
    return {
      data: '<% task(end).result.content.data %>'
    }
  } else if (nodeType === 'switch') {
    return {
      condition: `<% task(${node.id}).result.content.condition %>`
    }
  } else {
    let publishObj = {}
    for (const variable of node.data.output) {
      publishObj[
        variable.value
      ] = `<% task(${node.id}).result.content.${variable.value} %>`
    }
    return publishObj
  }
}

const getSuccessByNodeType = (nodeType, node, edgeInfoIdMap) => {
  if (nodeType === 'end') {
    return undefined
  } else if (nodeType === 'switch') {
    const nodePortItemsIdTypeMap = node.ports.items.reduce((prev, item) => {prev[item.id] = item.group ;return prev}, {})
    let leftNodeId = '' 
    let rightNodeId = '' 
    for (const edge of node.outgoingEdges) {
      if (nodePortItemsIdTypeMap[edge.data.sourcePortId] === 'left') {
        leftNodeId = edge.data.data.source
      }
      if (nodePortItemsIdTypeMap[edge.data.sourcePortId] === 'right') {
        rightNodeId = edge.data.data.source
      }
    }
    return {
      n: [
        {
          [rightNodeId]: `<% task(${node.id}).result.content.condition = 'n' %>`
        }
      ],
      y: [
        {
          [leftNodeId]: `<% task(${node.id}).result.content.condition = 'y' %>`
        }
      ]
    }
  } else {
    return {
      default: node?.outgoingEdges?.map?.(
        edge => edgeInfoIdMap.get(edge.id).target
      )
    }
  }
}

const formatDataBeforeCommit = ({ edges, nodes }) => {
  const nodeInfoIdMap = new Map()
  for (const node of nodes) {
    nodeInfoIdMap.set(node.id, node.data)
  }
  const edgeInfoIdMap = new Map()
  for (const edge of edges) {
    edgeInfoIdMap.set(edge.id, edge.data)
  }
  // 根据开始节点 获取 input 和 schemas
  let input = []
  let schemas = { properties: {}, required: [] }
  for (const variable of nodeInfoIdMap.get('start').variables.value) {
    input.push(variable.name)
    schemas.properties[variable.name] = variable
    variable.required && schemas.required.push(variable.name)
  }

  let tasks = {}

  for (const node of nodes) {
    const nodeType = node.data.nodeType

    tasks[node.id] = {
      nick: node.data.display,
      action: getActionByNodeType(nodeType, node),
      publish: getPublishByNodeType(nodeType, node),
      'on-success': getSuccessByNodeType(nodeType, node, edgeInfoIdMap)
    }
  }

  return {
    input,
    schemas,
    tasks
  }
}

export { formatDataBeforeCommit }
